<?php

/**
 * Формы для управления комментариями
 */
class CommentsForm extends Zend_Form {

    /**
     * Форма настройки секции с комментариями
     */
    public function settings($sid) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('comments');

        $approve = new Zend_Form_Element_Checkbox('approve');
        $approve->setLabel('Approve')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $anonymous = new Zend_Form_Element_Checkbox('anonymous');
        $anonymous->setLabel('Anonymous comments')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $model = new PageModel();
        $section = new Zend_Form_Element_Select('section');
        $current_section = $model->getSection($sid);
        $sections = $model->getSectionsList($current_section->pid);
        unset($sections[$sid]);
        $section->addMultiOptions(array(0 => $t->_('Undefined')) + $sections)
                ->setLabel('Section')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($section, $anonymous, $submit));

        $this->setAction(ADMIN_URL . '/pages/modify/id/' . $sid . '/a/edit');
        $this->setDefaultTranslator($t);
        return $this;
    }

    public function comment($sid, $param) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('commentForm');
        
        $reply = new Zend_Form_Element_Hidden('reply');
        $reply->removeDecorator('Label')
                ->removeDecorator('HtmlTag')
                ->setValue(0);

        $comment = new Zend_Form_Element_Textarea('comment');
        $comment->setLabel('You comment')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Post comment');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($reply, $comment, $submit));

        $this->setAction(SITE_URL . '/ajax/sid/' . $sid . '/' . $param['identifier'] . '/' . $param['value']);
        $this->setDefaultTranslator($t);
        return $this;
    }

    public function anonymous($sid, $param) {
        $t = Zend_Registry::get('Zend_Locale');
        $this->setName('commentForm');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $reply = new Zend_Form_Element_Hidden('reply');
        $reply->removeDecorator('Label')
                ->removeDecorator('HtmlTag')
                ->setValue(0);

        $email = new Zend_Form_Element_Text('guest_email');
        $email->setLabel('E-mail')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('EmailAddress')
                ->setRequired();

        $comment = new Zend_Form_Element_Textarea('comment');
        $comment->setLabel('You comment')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $captcha = new Zend_Form_Element_Captcha('captcha', array(
                    'captcha' => array(
                        'captcha' => 'Image',
                        'wordLen' => 6,
                        'width' => 260,
                        'timeout' => 120,
                        'expiration' => 300,
                        'font' => PUBLIC_PATH . '/fonts/captcha.ttf',
                        'imgDir' => PUBLIC_PATH . '/tmp/',
                        'imgUrl' => '/tmp/',
                        'gcFreq' => 5
                    )
                ));

        $captcha->setLabel('Captcha');
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Post comment');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($reply, $name, $email, $comment, $captcha, $submit));

        $this->setAction(SITE_URL . '/ajax/sid/' . $sid . '/' . $param['identifier'] . '/' . $param['value']);
        $this->setDefaultTranslator($t);
        return $this;
    }

}